package mobidever.godutch.business;

import java.util.ArrayList;
import java.util.List;

import mobidever.godutch.business.base.BusinessBase;
import mobidever.godutch.database.sqlitedal.SQLiteDALUser;
import mobidever.godutch.model.ModelUser;
import android.content.ContentValues;
import android.content.Context;
import android.util.Log;

public class BusinessUser extends BusinessBase{
	private SQLiteDALUser mSqLiteDALUser;
	public BusinessUser(Context pContext) {
		super(pContext);
		mSqLiteDALUser = new SQLiteDALUser(pContext);
		Log.i("mylog","BusinessUser over");
	}
	public boolean InsertUser(ModelUser pInfo){
		boolean _Result = mSqLiteDALUser.InsertUser(pInfo);
		return _Result;
	}
	public boolean DeleteUserByUserID(int pUserID){
		String _Condition = " And UserID = "+pUserID;
		boolean _Result = mSqLiteDALUser.DeleteUser(_Condition);
		return _Result;
	}
	public boolean UpdateUserByUserID(ModelUser pModelUser){
		String _Condition = " UserID = "+pModelUser.getmUserID();
		boolean _Result = mSqLiteDALUser.UpdateUser(_Condition, pModelUser);
		return _Result;
	}
	private List<ModelUser> getUsers(String pCondition){
		return mSqLiteDALUser.getUsers(pCondition);
	}
	public ModelUser getModelUserByUserID(int pUserID){
		List<ModelUser> _List = mSqLiteDALUser.getUsers(" UserID = "+pUserID);
		if(_List.size() == 1){
			return _List.get(0);
		}else{
			return null;
		}
	}
	public List<ModelUser> getUserListByUserID(String[] pUserID){
		List<ModelUser> _List = new ArrayList<ModelUser>();
		for(int i=0;i<pUserID.length;i++){
			_List.add(getModelUserByUserID(Integer.valueOf(pUserID[i])));
		}
		return _List;
	}
	public List<ModelUser> getNotHideUser(){
		List<ModelUser> _NotHideUser = mSqLiteDALUser.getUsers(" State = 1");
		return _NotHideUser;
	}
	public boolean IsExistUserByUserName(String pUserName,Integer pUserID){
		String _Condtion = " UserName = '"+pUserName + "'";
		if(pUserID != null){
			_Condtion += " And UserID <> "+pUserID;
		}
		List _List = mSqLiteDALUser.getUsers(_Condtion);
		if(_List.size()>0){
			return true;
		}else{
			return false; 
		}
	}
	public boolean HideUserByUserID(int pUserID){
		String _Condtion = " UserID = "+pUserID;
		ContentValues _ContentValues = new ContentValues();
		_ContentValues.put("State",0);
		boolean _Result = mSqLiteDALUser.UpdateUser(_Condtion, _ContentValues);
		if(_Result){
			return true;
		}else{
			return false;
		}
	}
	public String GetUserNameByUserID(String pUserID){
		List<ModelUser> _List = getUserListByUserID(pUserID.split(","));
		String _Name = "";
		for(int i=0;i<_List.size();i++){
			_Name += _List.get(i).getmUserName()+",";
		}
		return _Name;
	}
}
